package boot.controller.Shopping;

import boot.common.CommonResult;
import boot.domain.order.Order;
import boot.domain.wxGoods.Goods;
import boot.domain.wxGoodsCart.GoodsShopcart;
import boot.model.Orders;
import boot.model.userCarts;
import boot.model.userGoods;
import boot.model.wxResEntity;
import boot.service.orderService;
import boot.service.wxGoodService;
import boot.service.wxShoppingService;
import boot.service.wxUserService;
import boot.util.JwtUtil;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.swagger.models.auth.In;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class order {
    @Autowired
    private boot.service.wxGoodService wxGoodService;
    @Autowired
    private wxShoppingService wxShoppingService;
    @Autowired
    private orderService orderService;
    @Autowired
    private boot.service.wxUserService wxUserService;
    @GetMapping("/wxUser/getAllOrder")
    public CommonResult<List<Orders>> getAllOrder(@RequestHeader("Authorization") String token){
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            List<Order> orders = orderService.getAllOrder(userId);
            List<Orders> finals = new ArrayList<>();
            System.out.println(userId);
            for (Order item :
                    orders) {
                List<userGoods> goodsShopcarts = new ArrayList<>();
                Orders newOrder = new Orders();
                newOrder.setTime(item.getTime());
                String cartsId = item.getCartsId();
                System.out.println(cartsId);
                String[] strings = cartsId.split(",");
                for (String s :
                        strings) {
                    userGoods goods = new userGoods();
                    GoodsShopcart goodsShopcart = wxShoppingService.getCartById(Integer.parseInt(s));
                    int goodId = goodsShopcart.getGoodsId();
                    Goods goods1 = wxGoodService.getGoodById(goodId);
                    goods.setStatus((byte) 2);
                    goods.setNum(goodsShopcart.getNum());
                    goods.setGoods(goods1);
                    goodsShopcarts.add(goods);
                }

                newOrder.setGoodsShopcarts(goodsShopcarts);
                finals.add(newOrder);
            }
            return new CommonResult<>(200, "获取成功", finals);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "获取失败", null);
        }
    }

}
